/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.data.view;

import offset.nodes.server.view.list.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jcr.Property;
import javax.jcr.PropertyType;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import offset.nodes.server.controller.AttributeConstants;

/**
 *
 * @author Walter Lütgenau
 */
public class PropertyWrapper {
    static String[] propertyTypeNames = new String[]{
            "undefined",
            "string",
            "binary",
            "long",
            "double",
            "date",
            "boolean",
            "name",
            "path",
            "reference"
        };

    Property property;

    public PropertyWrapper(Property property) {
        this.property = property;
    }
    
    public boolean isReferenceType() {
        try {
            return property.getType() == PropertyType.REFERENCE;
        } catch (RepositoryException ex) {
            Logger.getLogger(PropertyWrapper.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    
    public String getReference() {
        try {
            if (property.getType() == PropertyType.REFERENCE && !property.getDefinition().isMultiple())
                return "?" + AttributeConstants.ATT_DISPATCHER_REFERENCE + "=" + property.getString();
            else
                return getString();
        } catch (RepositoryException ex) {
            Logger.getLogger(PropertyWrapper.class.getName()).log(Level.SEVERE, null, ex);
            return "";
        }
    }

    public String getString() {
        try {
            if (property.getDefinition().isMultiple()) {
                if ( property.getValues().length > 0)
                    return formatValues(property.getValues());
                else
                    return "";
            }
            return property.getString();
        } catch (RepositoryException ex) {
            Logger.getLogger(IteratorList.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public int getPropertyType() {
        try {
            return property.getDefinition().getRequiredType();
        } catch (RepositoryException ex) {
            Logger.getLogger(PropertyWrapper.class.getName()).log(Level.SEVERE, null, ex);
            return -1;
        }
    }

    public String getPropertyTypeName() {
        try {
            return propertyTypeNames[property.getDefinition().getRequiredType()];
        } catch (Exception ex) {
            Logger.getLogger(PropertyWrapper.class.getName()).log(Level.SEVERE, null, ex);
            return "";
        }
    }

    protected String formatValues(Value[] values) throws RepositoryException {
        StringBuffer buf = new StringBuffer();
        for( int i=0; i<values.length; i++) {
            if ( i > 0)
                buf.append(" + ");
            buf.append(values[i].getString());
        }
        
        return buf.toString();
    }

    public String getName() {
        try {
            return property.getName();
        } catch (RepositoryException ex) {
            Logger.getLogger(IteratorList.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
    }

